package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * ClassName: JsonResult
 * Package: cn.tedu.csmall.product.web
 * Description 响应结果类
 *
 * @version 0.0.1
 * @Author: 大傻
 * @Create:2022/11/30
 */
@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult {

    /**
     * 业务状态码
     */
    private Integer state;
    /**
     * 操作失败时的提示消息
     */

    private String message;
    /**
     * 操作成功时候相应数据
     */
    private Object data;

    public static JsonResult ok(){
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.state = ServiceCode.OK.getValue();
        return ok(null);
    }
    public static JsonResult ok(Object data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.state = ServiceCode.OK.getValue();
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e){
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.state = serviceCode.getValue();
//        jsonResult.message = message;
        return JsonResult.fail(e.getServiceCode(),e.getMessage());
    }
    public static JsonResult fail(ServiceCode serviceCode,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.state = serviceCode.getValue();
        jsonResult.message = message;
        return jsonResult;
    }
}
